package cn.ai.shoes.client.service.impl;

import cn.ai.shoes.client.entity.OssUploadLog;
import cn.ai.shoes.client.entity.vo.OssUploadLogPageVo;
import cn.ai.shoes.client.entity.vo.OssUploadLogVo;
import cn.ai.shoes.client.mapper.OssUploadMapper;
import cn.ai.shoes.client.service.OssUploadLogService;
import cn.ai.shoes.common.core.mybatis.LambdaQueryWrapperX;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

@Service
public class OssUploadLogServiceImpl extends ServiceImpl<OssUploadMapper, OssUploadLog> implements OssUploadLogService {

    @Override
    public IPage<OssUploadLogVo> pageOssUploadLogs(OssUploadLogPageVo imageQueryVo) {
        Page<OssUploadLog> page = new Page<>(imageQueryVo.getPageNum(), imageQueryVo.getPageSize());
        LambdaQueryWrapperX<OssUploadLog> queryWrapper = new LambdaQueryWrapperX<OssUploadLog>()
                .eqIfPresent(OssUploadLog::getUserId, imageQueryVo.getUserId())
                .likeIfPresent(OssUploadLog::getNickname, imageQueryVo.getNickname())
                .betweenIfPresent(OssUploadLog::getCreateTime, imageQueryVo.getBegin(), imageQueryVo.getEnd());

        IPage<OssUploadLog> resultPage = this.page(page, queryWrapper);

        return resultPage.convert(ossUploadLog -> {
            OssUploadLogVo vo = new OssUploadLogVo();
            BeanUtil.copyProperties(ossUploadLog, vo);
            return vo;
        });
    }

    @NotNull
    private static LambdaQueryWrapperX<OssUploadLog> getImageUploadLogLambdaQueryWrapperX(OssUploadLogPageVo imageQueryVo) {
        LambdaQueryWrapperX<OssUploadLog> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eqIfPresent(OssUploadLog::getUserId, imageQueryVo.getUserId())
                .likeIfPresent(OssUploadLog::getNickname, imageQueryVo.getNickname())
                .betweenIfPresent(OssUploadLog::getCreateTime, imageQueryVo.getBegin(), imageQueryVo.getEnd());
        return queryWrapper;
    }
}
